﻿function MessageFolderViewModel(parent) {
    var self = this;

    // knockout variables
    self.folders = ko.observableArray([]);
    self.workingFolder = ko.observable(new MessageFolder({ }));
    self.newFolder = ko.observable(new MessageFolder({ }));
    self.workingMessageUser = ko.observable();
    self.workingMessage = ko.observable();
    self.messageViewMode = ko.observable("list");
    self.listViewMode = ko.observable(true);
    self.detailsViewMode = ko.observable(false);
    self.composeViewMode = ko.observable(false);
    self.folderId = ko.observable(0);
    self.messageId = ko.observable(0);
    self.ExpertMessageUser = ko.observable(false);
    self.companyMessageUser = ko.observable(false);
    self.projectMessageUser = ko.observable(false);
    self.senderTag = ko.observable();
    self.recipientsTags = ko.observableArray([]);
    self.mesSentAlertVisible = ko.observable(false);
    self.selectedMesExpertUsers = ko.observableArray([]);
    self.messageUser = ko.observable(new MessageUser({ }));
    self.replyVisible = ko.observable(false);

    //REPLY
    self.reply = function() {
        self.replyVisible(true);
        self.workingMessage(new Message({ }));
        self.recipientsTags([]);
        self.setSender();
        //set the last messageuser's sender as recipient
        var sender = self.lastChild().Sender;
        self.recipientsTags.push({
                Id: sender.Id,
                Title: sender.Title,
                Type: sender.UserType,
                Photo: sender.Photo,
                Description: sender.Description
            });
        if (sender.UserType == "pp") {
            self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: sender.Id, IsSender: false, IsRead: false }));
        } else if (sender.UserType == "cp") {
            self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: sender.Id, IsSender: false, IsRead: false }));
        } else if (sender.UserType == "pr") {
            self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: sender.Id, IsSender: false, IsRead: false }));
        }

    };

    self.sendReply = function() {
        self.workingMessage().Subject(self.workingMessageUser().Subject);
        self.workingMessage().ParentId(self.workingMessageUser().Children[0].Id);
        $.ajax({
                url: "/Message/Save",
                type: 'post',
                data: ko.toJSON(self.workingMessage()),
                contentType: 'application/json',
                success: function(id) {
                    self.loadFolders();
                    self.replyVisible(false);
                    self.mesSentReplyAlertVisible(true);
                    setTimeout(function() { self.mesSentReplyAlertVisible(false); }, 3000);
                }
            });
    };

    self.discardReply = function() {
        self.replyVisible(false);
    };

//    self.replyToAll = function () {
//        self.replyVisible(true);
//        self.workingMessage(new Message({}));
//        self.recipientsTags([]);
//        self.setSender();
//        //set recipients
//        var recipients = new Array();
//        var sender = self.lastChild().Sender;
//        self.recipientsTags.push({
//            Id: sender.Id,
//            Title: sender.Title,
//            Type: sender.Type,
//            Photo: sender.Photo,
//            Description: sender.Description
//        });
//        if (sender.Type == "pp") {
//            self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: sender.Id, IsSender: false, IsRead: false }));
//        } else if (sender.Type == "cp") {
//            self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: sender.Id, IsSender: false, IsRead: false }));
//        } else if (sender.Type == "pr") {
//            self.workingMessage().MessagePositionUsers.push(new MessagePositionUser({ PositionId: sender.Id, IsSender: false, IsRead: false }));
//        }

//    };

    self.loadInbox = function() {
        self.loadFolders();
        self.setInboxOnStart();
    };
    self.numberMesInbox = ko.observable(0);
    self.numberMesOutbox = ko.observable(0);
    self.numberMesTrash = ko.observable(0);

    self.folderNameExistsAlertVisible = ko.observable(false);
    self.saveLabel = function() {
        $.ajax({
                url: "/MessageFolder/Save",
                type: 'post',
                data: ko.toJSON(self.newFolder()),
                contentType: 'application/json',
                success: function(result) {
                    if (result == "Ok") {
                        $('#CreateLabelDialog').modal('hide');
                        self.folderNameExistsAlertVisible(false);
                        self.loadFolders();
                    } else {
                        self.folderNameExistsAlertVisible(true);
                    }
                }
            });
    };
    self.cancelLabel = function() {
        $('#CreateLabelDialog').modal('hide');
        self.folderNameExistsAlertVisible(false);
    };

    self.setSender = function() {
        $.ajax({
                url: "/Message/GetSender",
                type: 'post',
                contentType: 'application/json',
                success: function(senders) {
                    var sender = senders[0];
                    self.senderTag({ Id: sender.Id, Title: sender.Title, Type: sender.Type, Photo: sender.Photo, Description: sender.Description });
                    if (sender.Type == "pp") {
                        self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: sender.Id, IsSender: true, IsRead: true }));
                    } else if (sender.Type == "cp") {
                        self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: sender.Id, IsSender: true, IsRead: true }));
                    } else if (sender.Type == "pr") {
                        self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: sender.Id, IsSender: true, IsRead: true }));
                    }
                }
            });
    };


    self.setExpertRecipient = function(profile) {
        self.selectNewMessage();
        self.recipientsTags.push({ Id: profile.Id, Title: profile.Title, Type: "pp" });
        self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: profile.Id, IsSender: false, IsRead: false }));
    };
    self.setCompanyRecipient = function(company) {
        self.selectNewMessage();
        self.recipientsTags.push({ Id: company.Id, Title: company.Title, Type: "cp" });
        self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: company.Id, IsSender: false, IsRead: false }));
    };
    self.setProjectRecipient = function(project) {
        self.selectNewMessage();
        self.recipientsTags.push({ Id: project.Id, Title: project.Title, Type: "pp" });
        self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: project.Id, IsSender: false, IsRead: false }));
    };

    self.setMessagesRoute = function() {
        location.hash = "#/Messages/?";
        self.setInboxOnStart();
    };
    self.setInboxOnStart = function() {
        if (self.folders().length != 0) {
            self.workingFolder($.Enumerable.From(self.folders()).SingleOrDefault(null, function(x) {
                return 0 === x.FolderType();
            }));
        }
    };

    self.selectFolder = function(folder) {
        self.workingFolder(folder);
        self.folderId(self.workingFolder().Id());
        self.messageViewMode("list");
        self.generateRoute();
    };

    self.selectMessageUser = function (messageUser) {
        //reset self.messageUser
        self.messageUser(new MessageUser({}));
        for (var i = 0; i < messageUser.Receivers.length; i++) {
            if (messageUser.Receivers[i].UserType == "pp") {
                self.messageUser().MessageExpertUserIds.push(messageUser.Receivers[i].Id);
            }
            else if (messageUser.Receivers[i].UserType == "cp") {
                self.messageUser().MessageCompanyUserIds.push(messageUser.Receivers[i].Id);
            }
            else if (messageUser.Receivers[i].UserType == "pr") {
                self.messageUser().MessageProjectUserIds.push(messageUser.Receivers[i].Id);
            }
        }
        $.ajax({
            url: "/Message/SetAsRead",
            type: 'post',
            data: ko.toJSON(self.messageUser()),
            contentType: 'application/json',
            success: function () {
                self.loadFolders();
                self.workingMessageUser(messageUser);
                self.messageId(self.workingMessageUser().Id);
                self.messageViewMode("details");
                self.generateRoute();
            }
        });

    };

    self.selectNewMessage = function() {
        self.workingMessage(new Message({ }));
        self.recipientsTags([]);
        self.messageViewMode("compose");
        self.generateRoute();
        self.setSender();
    };

    self.lastChild = ko.observable(null);
    //insert logic if there are no children
    self.setChildren = ko.computed(function() {
        if (self.workingFolder()) {
            if (self.workingMessageUser()) {
                if (self.workingMessageUser().Children.length != 0) {
                    self.lastChild(self.workingMessageUser().Children[self.workingMessageUser().Children.length - 1]);
                    var photo = self.lastChild().Sender.Photo;
                    self.lastChild().Sender.Photo = photo.replace("/250X250_", "/50X50_");
                    self.workingMessageUser().Children = self.workingMessageUser().Children.slice(0, self.workingMessageUser().Children.length - 1);
                    for (var i = 0; i < self.workingMessageUser().Children.length; i++) {
                        var _photo = self.workingMessageUser().Children[i].Sender.Photo;
                        self.workingMessageUser().Children[i].Sender.Photo = _photo.replace("/250X250_", "/50X50_");
                    }
                } else {
                    self.lastChild(self.workingMessageUser());
                }
            }
        }
    });

    self.send = function() {
        $.ajax({
                url: "/Message/Save",
                type: 'post',
                data: ko.toJSON(self.workingMessage()),
                contentType: 'application/json',
                success: function(id) {
                    self.workingMessage().Id(id);
                    self.messageViewMode("list");
                    self.generateRoute();
                    self.loadFolders();
                    self.mesSentAlertVisible(true);
                    setTimeout(function() { self.mesSentAlertVisible(false); }, 3000);
                }
            });
    };

    self.discard = function() {
        self.messageViewMode("list");
        self.generateRoute();
    };
    //MOVE TO TRASH/DELETE
    self.selectedMessages = ko.observableArray([]);
    self.selectedMesExpertUserIds = ko.observableArray([]);
    self.selectedMesCompanyUserIds = ko.observableArray([]);
    self.selectedMesProjectUserIds = ko.observableArray([]);

    self.checkSelectedMessages = function() {
        for (var i = 0; i < self.workingFolder().Messages().length; i++) {
            if (self.workingFolder().Messages()[i].IsChecked) {
                self.selectedMessages.push(self.workingFolder().Messages()[i]);
            }
        }
    };
    self.moveTo = function() {
        for (var x = 0; x < self.selectedMessages().length; x++) {
            var message = self.selectedMessages()[x];
            //if message belongs to inbox
            if (self.workingFolder().FolderType() == 0) {
                for (var i = 0; i < message.Receivers.length; i++) {
                    if (message.Receivers[i].UserType == "pp") {
                        self.messageUser().MessageExpertUserIds.push(message.Receivers[i].Id);
                    } else if (message.Receivers[i].UserType == "cp") {
                        self.messageUser().MessageCompanyUserIds.push(message.Receivers[i].Id);
                    } else {
                        self.messageUser().MessageProjectUserIds.push(message.Receivers[i].Id);
                    }
                }
            }
                //if message belongs to outbox
            else if (self.workingFolder().FolderType() == 1) {
                if (message.Sender.UserType == "pp") {
                    self.messageUser().MessageExpertUserIds.push(message.Sender.Id);
                } else if (message.Sender.UserType == "cp") {
                    self.messageUser().MessageCompanyUserIds.push(message.Sender.Id);
                } else {
                    self.messageUser().MessageProjectUserIds.push(message.Sender.Id);
                }
            }
                //if message belongs to trash = > check for sender and receivers since message can be moved to trash from outbox and inbox
            else if (self.workingFolder().FolderType() == 2) {
                if (message.Sender.UserType == "pp") {
                    self.messageUser().MessageExpertUserIds.push(message.Sender.Id);
                } else if (message.Sender.UserType == "cp") {
                    self.messageUser().MessageCompanyUserIds.push(message.Sender.Id);
                } else {
                    self.messageUser().MessageProjectUserIds.push(message.Sender.Id);
                }
                for (var z = 0; z < message.Receivers.length; z++) {
                    if (message.Receivers[z].UserType == "pp") {
                        self.messageUser().MessageExpertUserIds.push(message.Receivers[z].Id);
                    } else if (message.Receivers[z].UserType == "cp") {
                        self.messageUser().MessageCompanyUserIds.push(message.Receivers[z].Id);
                    } else {
                        self.messageUser().MessageProjectUserIds.push(message.Receivers[z].Id);
                    }
                }
            }
        }
    };

    self.moveToTrash = function() {
        self.checkSelectedMessages();
        if (self.selectedMessages().length != 0) {
            self.moveTo();
        }
        //if message belongs to inbox or outbox
        if (self.workingFolder().FolderType() == 0 || self.workingFolder().FolderType() == 1) {
            $.ajax({
                    url: "/Message/MoveToTrash",
                    type: 'post',
                    data: ko.toJSON(self.messageUser()),
                    contentType: 'application/json',
                    success: function() {

                    }
                });
        }
        //if message belongs to trash
        else if (self.workingFolder().FolderType() == 2) {
            $.ajax({
                    url: "/Message/DeleteMessage",
                    type: 'post',
                    data: ko.toJSON(self.messageUser()),
                    contentType: 'application/json',
                    success: function() {

                    }
                });
        }
        //reset lists of messageuserids
        self.messageUser(new MessageUser({ }));
        self.loadFolders();
    };

    self.moveToFolder = function(folder) {
        self.checkSelectedMessages();
        if (self.selectedMessages().length != 0) {
            self.moveTo();
        }
        self.messageUser().FolderName(folder.Name());
        //move to folder
        $.ajax({
                url: "/Message/MoveToFolder",
                type: 'post',
                data: ko.toJSON(self.messageUser()),
                contentType: 'application/json',
                success: function() {

                }
            });
        //reset lists of messageuserids
        self.messageUser(new MessageUser({ }));
        self.loadFolders();
    };


    self.inspectViewMode = ko.computed(function() {
        var mode = self.messageViewMode();
        if (mode == "list") {
            self.listViewMode(true);
            self.detailsViewMode(false);
            self.composeViewMode(false);
        } else if (mode == "details") {
            self.listViewMode(false);
            self.detailsViewMode(true);
            self.composeViewMode(false);
        } else if (mode == "compose") {
            self.listViewMode(false);
            self.detailsViewMode(false);
            self.composeViewMode(true);
        }
    });

    self.generateRoute = function() {
        var folderId = self.folderId();
        var messageId = self.messageId();
        var mode = self.messageViewMode();
        var route = '#/Messages/?';
        var defaultRoute = route;
        if (folderId != null) {
            route = route + (defaultRoute != route ? '&' : '') + 'folderId=' + self.folderId();
        }
        if (messageId != null) {
            route = route + (defaultRoute != route ? '&' : '') + 'messageId=' + self.messageId();
        }
        if (mode != null && mode != "") {
            route = route + (defaultRoute != route ? '&' : '') + 'mode=' + self.messageViewMode();
        }

        location.hash = route;
    };

    self.resetSearchModel = function() {
        self.messageViewMode("list");
        self.folderId(0);
        self.messageId(0);
    };

    self.loadFolders = function() {
        //if(self.messageViewMode() == "list"){
        $.ajax({
            url: "/api/MessageFolder/List",
            type: 'POST',
            dataType: 'json',
            data: {},
            contentType: "application/json",
            success: function (data) {
                var mappedFolders = $.map(data, function (item) { return new MessageFolder(item); });
                self.folders(mappedFolders);
                //self.numberMesInbox(data.unreadMesInboxCount);
                //self.numberMesOutbox(data.unreadMesOutboxCount);
                //self.numberMesTrash(data.unreadMesTrashCount);
            }
        });
        //$.getJSON("/MessageFolder/List", function(data) {
        //    var mappedFolders = $.map(data.folders, function(item) { return new MessageFolder(item); });
        //    self.folders(mappedFolders);
        //    self.numberMesInbox(data.unreadMesInboxCount);
        //    self.numberMesOutbox(data.unreadMesOutboxCount);
        //    self.numberMesTrash(data.unreadMesTrashCount);
        //});
        //};
    };

    self.autocompleteSenders = function() {
        $("#senderAutocomplete").autocomplete({
                source: function(request, response) {
                    $.ajax({
                            url: "Message/GetSenderAutocomplete",
                            dataType: "json",
                            data: {
                                searchTerm: request.term,
                                maxRows: 8
                            },
                            success: function(data) {
                                response($.map(data, function(item) {
                                    return {
                                        label: item.Title,
                                        value: item.Type + item.Id
                                    };
                                }));
                            }
                        });
                },
                minLength: 0,
                select: function(event, ui) {
                    var title = ui.item.label;
                    var id = ui.item.value.substring(2);
                    var type = ui.item.value.substring(0, 2);

                    self.senderTag({ Id: id, Title: title, Type: type });
                    if (type == "pp") {
                        self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: id, IsSender: true, IsRead: true }));
                    } else if (type == "cp") {
                        self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: id, IsSender: true, IsRead: true }));
                    } else if (type == "pr") {
                        self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: id, IsSender: true, IsRead: true }));
                    }

                    $("#senderAutocomplete").val("");
                    return false;
                },
                focus: function(event, ui) {
                    this.value = ui.item.label;
                    event.preventDefault(); // Prevent the default focus behavior.
                }
            });
    };
    self.showAllSenders = function() {
        $("#senderAutocomplete").autocomplete("search", "");
        $("#senderAutocomplete").focus();
    };
    self.autocompleteSendersReply = function() {
        $("#senderReplyAutocomplete").autocomplete({
                source: function(request, response) {
                    $.ajax({
                            url: "Message/GetSenderAutocomplete",
                            dataType: "json",
                            data: {
                                searchTerm: request.term,
                                maxRows: 8
                            },
                            success: function(data) {
                                response($.map(data, function(item) {
                                    return {
                                        label: item.Title,
                                        value: item.Type + item.Id
                                    };
                                }));
                            }
                        });
                },
                minLength: 0,
                select: function(event, ui) {
                    var title = ui.item.label;
                    var id = ui.item.value.substring(2);
                    var type = ui.item.value.substring(0, 2);

                    self.senderTag({ Id: id, Title: title, Type: type });
                    if (type == "pp") {
                        self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: id, IsSender: true, IsRead: true }));
                    } else if (type == "cp") {
                        self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: id, IsSender: true, IsRead: true }));
                    } else if (type == "pr") {
                        self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: id, IsSender: true, IsRead: true }));
                    }

                    $("#senderReplyAutocomplete").val("");
                    return false;
                },
                focus: function(event, ui) {
                    this.value = ui.item.label;
                    event.preventDefault(); // Prevent the default focus behavior.
                }
            });
    };
    self.showAllSendersReply = function() {
        $("#senderReplyAutocomplete").autocomplete("search", "");
        $("#senderReplyAutocomplete").focus();
    };
    self.removeSenderTag = function(tag) {
        self.senderTag(null);
    };

    self.autocompleteRecipients = function () {
        $("#recipientsAutocomplete").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Message/GetRecipientsAutocomplete",
                    dataType: "json",
                    data: {
                        searchTerm: request.term,
                        maxRows: 8
                    },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.Title,
                                value: item.Type == "pp" || item.Type == "cp" || item.Type == "pr"
                                             ? item.Type + item.Id
                                //Members is list of Expert/Company/Position ids(profiles that belong to certain list)
                                             : item.Members
                            };
                        }));
                    }
                });
            },
            minLength: 0,
            select: function (event, ui) {
                var title = ui.item.label;
                var id;
                var type;
                var members;
                if (title.indexOf("<Expert Profile>") != -1 || title.indexOf("<Company Profile>") != -1 || title.indexOf("<Position/Position Profile>") != -1) {
                    id = ui.item.value.substring(2);
                    type = ui.item.value.substring(0, 2);
                }
                else {
                    members = ui.item.value;
                }
                var found = ko.utils.arrayFirst(self.recipientsTags(), function (item) {
                    return item.Title == title;
                });
                if (!found) {
                    self.recipientsTags.push({ Id: id, Title: title, Type: type });
                    if (type == "pp") {
                        self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: id, IsSender: false, IsRead: false }));
                    } else if (type == "cp") {
                        self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: id, IsSender: false, IsRead: false }));
                    } else if (type == "pr") {
                        self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: id, IsSender: false, IsRead: false }));
                    } else if (title.indexOf("<Expert List>") != -1) {
                        for (var i = 0; i < members.length; i++) {
                            self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: members[i], IsSender: false, IsRead: false }));
                        }
                    } else if (title.indexOf("<Company List>") != -1) {
                        for (var i = 0; i < members.length; i++) {
                            self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: members[i], IsSender: false, IsRead: false }));
                        }
                    } else if (title.indexOf("<Position List>") != -1) {
                        for (var i = 0; i < members.length; i++) {
                            self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: members[i], IsSender: false, IsRead: false }));
                        }
                    }
                }

                $("#recipientsAutocomplete").val("");
                return false;
            },
            focus: function (event, ui) {
                this.value = ui.item.label;
                event.preventDefault(); // Prevent the default focus behavior.
            }
        });
    };
    self.showAllRecipients = function() {
        $("#recipientsAutocomplete").autocomplete("search", "");
        $("#recipientsAutocomplete").focus();
    };
    self.autocompleteRecipientsReply = function() {
        $("#recipientsReplyAutocomplete").autocomplete({
                source: function(request, response) {
                    $.ajax({
                            url: "Message/GetRecipientsAutocomplete",
                            dataType: "json",
                            data: {
                                searchTerm: request.term,
                                maxRows: 8
                            },
                            success: function(data) {
                                response($.map(data, function(item) {
                                    return {
                                        label: item.Title,
                                        value: item.Type + item.Id
                                    };
                                }));
                            }
                        });
                },
                minLength: 0,
                select: function(event, ui) {
                    var title = ui.item.label;
                    var id = ui.item.value.substring(2);
                    var type = ui.item.value.substring(0, 2);

                    var found = ko.utils.arrayFirst(self.recipientsTags(), function(item) {
                        return item.Title == title;
                    });
                    if (!found) {
                        self.recipientsTags.push({ Id: id, Title: title, Type: type });
                        if (type == "pp") {
                            self.workingMessage().MessageExpertUsers.push(new MessageExpertUser({ ExpertProfileId: id, IsSender: false, IsRead: false }));
                        } else if (type == "cp") {
                            self.workingMessage().MessageCompanyUsers.push(new MessageCompanyUser({ CompanyProfileId: id, IsSender: false, IsRead: false }));
                        } else if (type == "pr") {
                            self.workingMessage().MessageProjectUsers.push(new MessageProjectUser({ ProjectId: id, IsSender: false, IsRead: false }));
                        }
                    }

                    $("#recipientsReplyAutocomplete").val("");
                    return false;
                },
                focus: function(event, ui) {
                    this.value = ui.item.label;
                    event.preventDefault(); // Prevent the default focus behavior.
                }
            });
    };
    self.showAllRecipientsReply = function() {
        $("#recipientsReplyAutocomplete").autocomplete("search", "");
        $("#recipientsReplyAutocomplete").focus();
    };
    self.removeRecipientTag = function(tag) {
        self.recipientsTags.remove(tag);
        if (tag.Type == "pp") {
            self.workingMessage().MessageExpertUsers.remove(function(item) { return item.ExpertProfileId() == tag.Id; });
        } else if (tag.Type == "cp") {
            self.workingMessage().MessageExpertUsers.remove(function(item) { return item.CompanyProfileId() == tag.Id; });
        } else if (tag.Type == "pr") {
            self.workingMessage().MessageExpertUsers.remove(function(item) { return item.ProjectId() == tag.Id; });
        }
    };
}